package com.cocola.service;

import com.cocola.mapper.UnitMapper;
import com.cocola.pojo.Declaration;
import com.cocola.pojo.Unit;
import com.cocola.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UnitService {
    @Autowired
    private UnitMapper unitMapper;

    @Autowired
    private DeclarationService declarationService;

    public List<Unit> findAllUnit() {
        return unitMapper.selectList(null);
    }

    /*
    // 返回 Unit 不能为空，否则空错误,所以它不能用于判断
    public Unit findOneUnit(String unitName) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("unit_name", unitName);

        return unitMapper.selectOne(wrapper);
    }
    */

    /*
    ** 通过登陆的 unitAccount 查询 unitName
     */
    public List<Unit> findOneUnit(String unitAccount) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_account", unitAccount);

        return unitMapper.selectByMap(map);
    }

    /*
    **用户申请时判断申请的单位是否存在
     */
    public List<Unit> findOneUnit2(String unitName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_name", unitName);

        return unitMapper.selectByMap(map);
    }

    /*
    **登陆
     */
    public boolean login(String unitAccount, String password) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_account", unitAccount);
        List<Unit> unit = unitMapper.selectByMap(map);

        if(!unit.isEmpty()) {
            if(unit.get(0).getPassword().equals(password)) {
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }

    }

    /*
    **通过当前Unit:unit_name 找到申请者 Declaration:unit_send
    **申请表中的 unit_send 对应 unit 中的 unit_name
     */
    public List<Declaration> findDeclaration(String unitName) {
        return declarationService.findDeclaration2(unitName);
    }

    /*
    **审核申请，修改申请状态
    **但是这里总感觉怪怪的
     */
    public void updateStatus(String status, String unitName) {
        declarationService.updateStatus(status, unitName);
    }


    /**
     * insert
     */
    public void register(String unitAccount, String password, String passwordConfirm,
                    String unitName, String legalPerson, String unitLeval, String unitType,
                    String unitIndustry, String register, String address, String addressDetail,
                    String mandator, String businessLicense, String photo, String signature, String userIdcard) {


        Unit unit = new Unit();
        unit.setUnitAccount(unitAccount);
        unit.setPassword(password);
        unit.setPasswordConfirm(passwordConfirm);
        unit.setUnitName(unitName);
        unit.setLegalPerson(legalPerson);
        unit.setUnitLeval(legalPerson);
        unit.setUnitType(unitType);
        unit.setUnitIndustry(unitIndustry);
        unit.setRegister(register);
        unit.setAddress(address);
        unit.setAddressDetail(addressDetail);
        unit.setMandator(mandator);
        unit.setBusinessLicense(businessLicense);
        unit.setSignature(signature);
        unit.setUserIdcard(userIdcard);
        unitMapper.insert(unit);
        System.out.println("add unit done===================>");
    }

}
